package com.googlecode.linkmein.domain;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.photos.UserFeed;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.util.ServiceException;

import java.net.URL;
import java.io.IOException;

import groovy.lang.GString;
import org.apache.commons.lang.StringUtils;

public class Albums extends java.util.ArrayList<Album> {
    public static Albums find(PicasawebService service, String googleId) throws IOException, ServiceException {
        String url = String.format("http://picasaweb.google.com/data/feed/api/user/%s?kind=album", googleId);
        Albums albums = new Albums();
        try {
            UserFeed userfeed = service.getFeed(new URL(url), UserFeed.class);
            for (AlbumEntry albumEntry : userfeed.getAlbumEntries()) {
                String link = albumEntry.getHtmlLink().getHref();
                String thumbnailurl = albumEntry.getMediaThumbnails().get(0).getUrl();
                String title = albumEntry.getTitle().getPlainText();
                albums.add(new Album(googleId, title, thumbnailurl, link));
            }
        } catch (Exception e) {
        }
        return albums;
    }

    public void loadAlbum(Employee employee) {
        for (Album album : this) {
            if (StringUtils.equals(album.getGoogleId(), employee.getGoogleId())) {
                employee.addAlbum(album);
            }
        }
    }
}